using System;
using System.Collections.Generic;
using NodeCanvas;
using NodeCanvas.DialogueTrees;
using UnityEngine;

public class StoryDialogController : MonoBehaviour
{
	[Serializable]
	public class ActorDef
	{
		public GGActorSetup.Actor actor;

		public StoryActorBase storyActor;
	}

	public ChoiceUI choiceUI;

	public AchivementDialogUI achivementUI;

	public DialogueTree dialogue;

	public Conversations conversations;

	public UILabel title;

	public List<ActorDef> actors = new List<ActorDef>();

	private DialogueActor talkingActor;

	private StoryActorBase storyActor;

	protected Action onComplete;

	private GGStoryAchivementBase achivement;

	private bool reportAchivement;

	private bool firstView;

	private int lookedAtParts;

	private bool skipped;

	public Blackboard blackboard
	{
		get
		{
			return conversations.gameObject.GetComponent<Blackboard>();
		}
	}

	public void ShowWithAchivement(GGStoryAchivementBase achivement, bool reportAchivement, bool push = true)
	{
		firstView = !achivement.isReported();
		achivement.ReportAll();
		DialogueTree dialogueTreeForAchivement = conversations.GetDialogueTreeForAchivement(achivement);
		if (!(dialogueTreeForAchivement == null))
		{
			dialogue = dialogueTreeForAchivement;
			this.achivement = achivement;
			this.reportAchivement = reportAchivement;
			if (push)
			{
				NavigationManager.instance.Push(base.gameObject);
			}
			title.text = achivement.name;
		}
	}

	public void SetOnComplete(Action onComplete)
	{
		this.onComplete = onComplete;
	}

	private void Awake()
	{
	}

	private void OnEnable()
	{
		foreach (ActorDef actor in actors)
		{
			actor.storyActor.blackboard = blackboard;
		}
		lookedAtParts = 0;
		skipped = false;
		NodeCanvas.EventHandler.Unsubscribe(this);
		dialogue.StopGraph();
		NodeCanvas.EventHandler.Subscribe(this, DLGEvents.OnActorSpeaking);
		NodeCanvas.EventHandler.Subscribe(this, DLGEvents.OnDialogueStarted);
		NodeCanvas.EventHandler.Subscribe(this, DLGEvents.OnDialogueFinished);
		NodeCanvas.EventHandler.Subscribe(this, DLGEvents.OnDialoguePaused);
		NodeCanvas.EventHandler.Subscribe(this, DLGEvents.OnDialogueOptions);
		blackboard.SetDataValue("PlayerName", GGPlayerSettings.instance.GetName());
		talkingActor = null;
		storyActor = null;
		dialogue.StartGraph();
		Ads.instance.hideBanner(true);
	}

	private void OnDisable()
	{
		NodeCanvas.EventHandler.Unsubscribe(this);
		Ads.instance.hideBanner(false);
		firstView = false;
	}

	private StoryActorBase GetStoryActor(DialogueActor actor)
	{
		GGActorSetup component = actor.GetComponent<GGActorSetup>();
		if (component == null)
		{
			return actors[0].storyActor;
		}
		GGActorSetup.Actor actor2 = component.actor;
		foreach (ActorDef actor3 in actors)
		{
			if (actor3.actor == actor2)
			{
				return actor3.storyActor;
			}
		}
		return actors[0].storyActor;
	}

	private void OnActorSpeaking(DialogueSpeechInfo speech)
	{
		Debug.Log("ON SPEAKING");
		bool changeActor = false;
		lookedAtParts++;
		if (talkingActor != speech.actor)
		{
			if (storyActor != null)
			{
				storyActor.Hide();
			}
			talkingActor = speech.actor;
			storyActor = GetStoryActor(speech.actor);
			changeActor = true;
		}
		if (storyActor != null)
		{
			storyActor.OnActorSpeaking(speech, changeActor);
		}
	}

	private void OnDialogueStarted(DialogueTree dialogue)
	{
	}

	private void OnDialoguePaused()
	{
		OnDialogueFinished();
	}

	private void OnDialogueFinished()
	{
		StopAllCoroutines();
		if (storyActor != null)
		{
			storyActor.Hide();
			choiceUI.gameObject.SetActive(false);
		}
		storyActor = null;
		if (firstView && achivement != null)
		{
			Analytics.instance.ReportStoryShow(achivement, skipped, lookedAtParts);
		}
		OnDone();
	}

	public void OnClick()
	{
		if (storyActor != null)
		{
			storyActor.OnClick();
		}
	}

	private void OnDialogueOptions(DialogueOptionsInfo optionsInfo)
	{
		choiceUI.ShowChoices(optionsInfo);
	}

	public void OnSkip()
	{
		dialogue.StopGraph();
		skipped = true;
		OnDialogueFinished();
	}

	private void OnDone()
	{
		if (reportAchivement)
		{
			achivementUI.ShowAchivement(achivement, delegate
			{
				NavigationManager.instance.Pop();
				if (onComplete != null)
				{
					onComplete();
				}
			});
		}
		else
		{
			NavigationManager.instance.Pop();
			if (onComplete != null)
			{
				onComplete();
			}
		}
	}
}
